package com.lm.scau.framedemos.Adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lm.scau.framedemos.BaseActivity;
import com.lm.scau.framedemos.R;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Transformation;

import java.util.List;

/**
 * Function:
 * auth：liming on 2016/7/20 11:23
 * mail：liming@wxchina.com
 */
public class ImageAdapter extends BaseAdapter {
    private Context context;
    private List<String> urlList;
    private LayoutInflater inflater;
   // private static final String url = "http://www.dpfile.com/s/res/app-touch-icon-152x152.ee6d0c24fc2de0f9a62b6cc9e6720393.png";

    public ImageAdapter(Context context, List urlList) {
        this.context = context;
        this.urlList = urlList;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return urlList.size();
    }

    @Override
    public Object getItem(int position) {
        return urlList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        viewHolder holder = null;
        if (null == convertView) {
            holder = new viewHolder();
            convertView = inflater.inflate(R.layout.item_main_list, parent, false);
            holder.icon = (ImageView) convertView.findViewById(R.id.icon_img);
            holder.head = (TextView) convertView.findViewById(R.id.head_text_view);
            holder.subHead = (TextView) convertView.findViewById(R.id.sub_head_text_view);
            convertView.setTag(holder);
        } else {
            holder = (viewHolder) convertView.getTag();
        }
        String url = urlList.get(position);
        //  Picasso.with(context).load(url).placeholder(R.drawable.icon_person_header).transform(transformation).into(holder.icon);
        Picasso.with(context).load(url).resize(160, 160).placeholder(R.drawable.icon_person_header).into(holder.icon);
        holder.head.setText("测试主标题");
        holder.subHead.setText("测试小标题");
        holder.icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        return convertView;
    }
    Transformation transformation = new Transformation() {

        @Override
        public Bitmap transform(Bitmap source) {
            int targetWidth = 160;

            double aspectRatio = (double) source.getHeight() / (double) source.getWidth();
            int targetHeight = (int) (targetWidth * aspectRatio);
            Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
            if (result != source) {
                // Same bitmap is returned if sizes are the same
                source.recycle();
            }
            return result;
        }

        @Override
        public String key() {
            return "transformation" + " desiredWidth";
        }
    };
    class viewHolder {
        public ImageView icon;
        public TextView head;
        public TextView subHead;
    }
}
